package my.ai;

import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Random;

public class AIEightQueen {

    private int ans = 0, p;        //暂时存储变量
    private int[] pos = new int[8];        //记录一组皇后位置
    private int[] pnow = new int[8];        //记录一组皇后位置
    private boolean[][] vis = new boolean[3][20];//记忆数组判断列,主对角线,副对角线是否被占

    public int[][] loca = new int[92][8];    //正确位置记录
    public int[] bar = new int[8];            //关卡设置

    public int conflict_x, conflict_y;        //记录当前冲突棋子

    //深搜回溯记录成功组合
    public void dfs(int cur) {
        if (cur > 8)//如果当前行数超过8(表明八个皇后已经放好)则结果加一，返回继续递归
        {
            for (int i = 0; i < 8; i++)
                loca[ans][i] = pos[i];
            ans++;
            return;
        }
        //vis[0][i]判断列,vis[i][cur-i+8]判断主对角线,vis[2][cur+i]判断副对角线
        for (int i = 1; i <= 8; i++)
            if (!vis[0][i] && !vis[1][cur - i + 8] && !vis[2][cur + i]) {
                pos[p++] = i;
                vis[0][i] = vis[1][cur - i + 8] = vis[2][cur + i] = true;
                dfs(cur + 1);//深度搜索
                vis[0][i] = vis[1][cur - i + 8] = vis[2][cur + i] = false;
                p--;
            }
    }

    //判断棋局
    public int match(int[][] ChessBoard) {
        int i, j, flag1 = 0, flag2 = 0;
        for (i = 0; i < 8; i++) pnow[i] = 0;
        for (i = 0; i < 8; i++)    //读取当前坐标
            for (j = 0; j < 8; j++)
                if (ChessBoard[i][j] != 0) {
                    flag1++;
                    pnow[i] = j + 1;
                }

        for (i = 0; i < 92; i++)    //查找相似组合
        {
            flag2 = 0;
            for (j = 0; j < 8; j++)
                if ((pnow[j] == loca[i][j]))
                    flag2++;
            if (flag2 >= flag1)
                return i;
        }
        return -1;        //未找到合适组合
    }

    //游戏提示
    public void showhint(int[][] ChessBoard) {
        // TODO: Add your control notification handler code here
        /*if(cont>=3)
		{
			AfxMessageBox("本局3次提示已消耗完毕！");
			return;
		}
		cont++;*/

        int result = match(ChessBoard);
		/*if(ans==-1)
			AfxMessageBox("死棋！");
		else
			flashPoint(ans);*/
    }

    //判断棋子是否有效： true--有效(即无冲突)  false--无效
    public boolean valid(int x, int y, int ChessBoard[][]) {
        int x1;
        int y1;
        //播放音效
        for (y1 = 0; y1 < 8; y1++)
            for (x1 = 0; x1 < 8; x1++)
                if ((ChessBoard[x1][y1] != 0) && (!((x1 == x) && (y1 == y)))) {
                    if (x1 == x) //判断行
                        return false;
                    if (y1 == y)//判断列
                        return false;
                    if (Math.abs(x1 - x) == Math.abs(y1 - y)) //判断斜线
                        return false;
                }
        return true;
    }

    //产生关卡布局场景函数
    public void BarProduce(int num) {
        Random rnd = new Random();
        int p = rnd.nextInt(92);
        for (int i = 0; i < 8; i++)        //随机取正确解答中的一组
        {
            bar[i] = loca[p][i];
        }
        for (int i = 0; i < num; i++)        //去除num项供玩家填充
        {
            p = rnd.nextInt(8);
            while (-1 == bar[p])        //若产生随机数重复，则重新产生，以保证与关卡相符
            {
                p = rnd.nextInt(8);
            }
            bar[p] = -1;
        }
    }

    public void order(int score) throws IOException {
        int[] jifen = new int[10];            //积分排行榜记录
        try {
            //---SD卡存储器---
            File sdCard = Environment.getExternalStorageDirectory();
            File directory = new File(sdCard.getAbsolutePath() + "/MyZone");
            directory.mkdirs();
            File file = new File(directory, "rank.txt");

            //读数据
            FileInputStream fIn = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(fIn);

            char[] inputBuffer = new char[1];
            int x = 0, tnum = 0;

            //String snum = String.copyValueOf(inputBuffer, 0, 1);
            //Toast.makeText(this.getContext(), "'"+snum+"'", Toast.LENGTH_SHORT).show();
            while (isr.read(inputBuffer) > 0) {
                String snum = String.copyValueOf(inputBuffer, 0, 1);
                if (snum.equals("\n")) {
                    jifen[x++] = tnum;
                    tnum = 0;
                } else {
                    tnum = tnum * 10 + Integer.parseInt(snum);
                }
            }

            //排序
            for (int i = 0; i < 8; i++) {
                if (score > jifen[i]) {
                    for (int j = 7; j > i; j--)
                        jifen[j] = jifen[j - 1];
                    jifen[i] = score;
                    break;
                }
            }

            //Toast.makeText(this.getContext(), "'"+jifen[0]+jifen[1]+jifen[2]+jifen[3]+jifen[4]+jifen[5]+jifen[6]+jifen[7]+"'", Toast.LENGTH_SHORT).show();

            //写数据
            FileOutputStream fOut = new FileOutputStream(file);
            OutputStreamWriter osw = new OutputStreamWriter(fOut);
            for (int i = 0; i < 8; i++) {
                osw.write(jifen[i] + "\n");
                //osw.write(0+"\n");
            }
            osw.flush();        //清空缓冲区数据
            osw.close();        //关闭文件
        } catch (IOException ioe) {
            File sdCard = Environment.getExternalStorageDirectory();
            File directory = new File(sdCard.getAbsolutePath() + "/MyZone");
            directory.mkdirs();
            File file = new File(directory, "rank.txt");
            //写数据
            FileOutputStream fOut = new FileOutputStream(file);
            OutputStreamWriter osw = new OutputStreamWriter(fOut);
            osw.write(score + "\n");
            osw.flush();        //清空缓冲区数据
            osw.close();        //关闭文件
            ioe.printStackTrace();
        }
    }
}
